package android;

import hibernate.Driverstab;
import hibernate.DriverstabDAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONException;
import org.json.JSONObject;



public class ForgetPassWordServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;


	public ForgetPassWordServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);

	}



	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		JSONObject jsonObj=null;
		String reciveEmail=null;

		try {
			jsonObj=new JSONObject(request.getParameter("jsonString"));
			reciveEmail=jsonObj.getString("email");

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	




		List<Driverstab> driversList=null;
		DriverstabDAO driverstabDAO=new DriverstabDAO();
		driverstabDAO.getSession().beginTransaction();
		try
		{

			String hql="from Driverstab as d where d.email like :email";
			Query result=driverstabDAO.getSession().createQuery(hql);


			System.out.println("get account  "+reciveEmail+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			result.setString("email",reciveEmail);
			driversList=result.list();
		}
		catch(HibernateException e)
		{
			System.out.println("hibernate error in LoginServlet");
			e.printStackTrace();
		}
		finally
		{
			driverstabDAO.getSession().getTransaction().commit();
			driverstabDAO.getSession().close();

		}
		JSONObject resultJson=null;
		resultJson =new JSONObject();
		boolean isFind=driversList.size()>0;
		try {
			if(isFind)
			{
				resultJson.put("findResult","accept");
				Driverstab driver=driversList.get(0);
				sendEmail(driver.getEmail(),driver.getPwd());
			}
			else
			{
				resultJson.put("findResult","none");
			}
			
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		OutputStream out=response.getOutputStream();
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	}


	private void sendEmail(String decEmail,String pass)
	{
		try
		{
			final Properties props = new Properties();
			/*
			 * 可用的属性： mail.store.protocol / mail.transport.protocol / mail.host /
			 * mail.user / mail.from
			 */
			// 表示SMTP发送邮件，需要进行身份验证
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.host", "smtp.126.com");
			// 发件人的账号
			props.put("mail.user", "bleuesprit@126.com");
			// 访问SMTP服务时需要提供的密码
			props.put("mail.password", "xh10358521");

			// 构建授权信息，用于进行SMTP进行身份验证
			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					// 用户名、密码
					String userName = props.getProperty("mail.user");
					String password = props.getProperty("mail.password");
					return new PasswordAuthentication(userName, password);
				}
			};
			// 使用环境属性和授权信息，创建邮件会话
			Session mailSession = Session.getInstance(props, authenticator);
			// 创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			// 设置发件人
			InternetAddress form = new InternetAddress(
					props.getProperty("mail.user"));
			message.setFrom(form);

			// 设置收件人
			InternetAddress to = new InternetAddress(decEmail);
			message.setRecipient(RecipientType.TO, to);

			//	        // 设置抄送
			//	        InternetAddress cc = new InternetAddress("luo_aaaaa@yeah.net");
			//	        message.setRecipient(RecipientType.CC, cc);
			//
			//	        // 设置密送，其他的收件人不能看到密送的邮件地址
			//	        InternetAddress bcc = new InternetAddress("aaaaa@163.com");
			//	        message.setRecipient(RecipientType.CC, bcc);

			// 设置邮件标题
			message.setSubject("有车位App忘记密码邮件");

			// 设置邮件的内容体
			message.setContent("pass:"+pass, "text/html;charset=UTF-8");

			// 发送邮件
			Transport.send(message);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}



}
